<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/6/29
 * Time: 11:55
 */

namespace PHPMailer;
use think\lang;

class SendEmail
{
    private $db_config;

    public function __construct($db='')
    {
        $this->db_config = $db;
    }


    /**
     * 邮件发送
     * @param $to 接收人
     * @param string $subject 邮件标题
     * @param string $content 邮件内容(html模板渲染后的内容)
     * @param array $maildata 更多数组设置
     *
     */
    public function SendEmail($to,$subject='我是主题！嘿嘿，测试成功！',$content='内容测试成功',$maildata=''){
//        echo $to;
//        echo $this->db_config['address']['value'];
//        echo $content;
//        return '消息已经发送';
//        exit();
//        echo cache('systemsystem' . cookie('think_var'))['title']['value'];
//        echo cookie('think_var');
//        exit();

        $mail = new PHPMailer(true);                    // Passing `true` enables exceptions
        try {
            //服务器设置
            $mail->SMTPDebug = 0;                                 // 开启调试模式
            $mail->isSMTP();                                      // 使用smtp鉴权方式发送邮件
            $mail->Host = cache('systemsystem' . cookie('think_var'))['emailHost']['value'];                          // SMTP 服务器
            $mail->SMTPAuth = true;                               // SMTP 这个必须是true
            $mail->Username = cache('systemsystem' . cookie('think_var'))['emailName']['value'];                 // SMTP 邮箱
            $mail->Password = cache('systemsystem' . cookie('think_var'))['emailpassword']['value'];                    // SMTP 密码
            $mail->SMTPSecure = cache('systemsystem' . cookie('think_var'))['emailSecure']['value'];                            // Enable TLS encryption, `ssl` also accepted
            $mail->Port = cache('systemsystem' . cookie('think_var'))['emailPort']['value'];                                    // TCP port to connect to
            $mail->CharSet = 'UTF-8';                             // 设置发送的邮件的编码
            $mail->setLanguage(cookie('think_var'));

            $mail->setFrom($mail->Username,cache('systemsystem' . cookie('think_var'))['title']['value']);            //发送人
            $mail->addAddress($to);     // 添加收件人
//            $mail->addAddress($to, '收件人名');     // 添加收件人
//            $mail->addReplyTo('zhiqiang@iottogo.com', 'Information');//回复地址
//            $mail->addCC('zhiqiang@iottogo.com');
//            $mail->addBCC('zhiqiang@iottogo.com');

            //附件
//            $mail->addAttachment('../public/uploads/file/5b2b18e3389af.txt');         // Add attachments
//            $mail->addAttachment('../public/uploads/case-img.jpg', 'new.jpg');    // Optional name

            //Content
            $mail->isHTML(true);                                  // Set email format to HTML
            $mail->Subject = $subject;
            $mail->Body    = $content;
//            $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

            $mail->send();
            return '1';
        } catch (Exception $e) {
            return $mail->ErrorInfo.cookie('think_var');
        }
    }

}